package com.nbot.server.controller;

import cn.hutool.Hutool;
import cn.hutool.core.date.DateUtil;
import com.nbot.server.domain.KeyParameterDO;
import com.nbot.server.domain.KeyParameterLogDO;
import com.nbot.server.service.KeyParameterLogService;
import com.nbot.server.service.KeyParameterService;
import com.nbot.server.util.PageUtils;
import com.nbot.server.util.Query;
import com.nbot.server.util.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 重要参数记录
 *
 * @author nbot
 * @email nbot@163.com
 * @date 2018-06-17 15:33:14
 */

@Controller
@RequestMapping("/server/keyParameterLog")
public class KeyParameterLogController {
    @Autowired
    private KeyParameterLogService keyParameterLogService;

    @Autowired
    private KeyParameterService keyParameterService;

    @GetMapping()
    @RequiresPermissions("server:keyParameterLog:keyParameterLog")
    String KeyParameterLog() {
        return "server/keyParameterLog/keyParameterLog";
    }

    @GetMapping("/{keyParameterAutoId}")
    @RequiresPermissions("server:keyParameterLog:keyParameterLog")
    String getKeyParameterLogByKeyParameterAutoId(@PathVariable("keyParameterAutoId") Long autoId, Model model) {
        model.addAttribute("kp_autoid", autoId);
        return "server/keyParameterLog/keyParameterLog";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("server:keyParameterLog:keyParameterLog")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<KeyParameterLogDO> keyParameterLogList = keyParameterLogService.list(query);
        for (KeyParameterLogDO kpLog : keyParameterLogList) {
            KeyParameterDO kp = keyParameterService.get(kpLog.getKeyParameterAutoId());
            if (kp != null) {
                kpLog.setItemDescription(kp.getItemDescription());
                kpLog.setGroupName(kp.getGroupName());

            }
        }
        int total = keyParameterLogService.count(query);
        PageUtils pageUtils = new PageUtils(keyParameterLogList, total);
        return pageUtils;
    }

    @ResponseBody
    @GetMapping("/getList/{keyParameterAutoId}")
    @RequiresPermissions("server:keyParameterLog:keyParameterLog")
    public R getList(@PathVariable("keyParameterAutoId") Long autoId) {
        //查询列表数据
        Map<String, Object> params = new HashMap<>();
        params.put("keyParameterAutoId", autoId);
        params.put("sort", "save_time");
        params.put("order", "asc");

        List<KeyParameterLogDO> keyParameterLogList = keyParameterLogService.list(params);
        List<String> keys = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for (KeyParameterLogDO kpLog : keyParameterLogList) {
            keys.add(DateUtil.formatDateTime(kpLog.getSaveTime()));
            values.add(kpLog.getKeyParameterValue().doubleValue());
        }
        KeyParameterDO kp = keyParameterService.get(autoId);

        return R.ok()
                .put("kpName", kp.getGroupName() + kp.getItemDescription())
                .put("keys", keys)
                .put("values", values);
    }

    @GetMapping("/add")
    @RequiresPermissions("server:keyParameterLog:add")
    String add() {
        return "server/keyParameterLog/add";
    }

    @GetMapping("/edit/{autoId}")
    @RequiresPermissions("server:keyParameterLog:edit")
    String edit(@PathVariable("autoId") Long autoId, Model model) {
        KeyParameterLogDO keyParameterLog = keyParameterLogService.get(autoId);
        model.addAttribute("keyParameterLog", keyParameterLog);
        return "server/keyParameterLog/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("server:keyParameterLog:add")
    public R save(KeyParameterLogDO keyParameterLog) {
        if (keyParameterLogService.save(keyParameterLog) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("server:keyParameterLog:edit")
    public R update(KeyParameterLogDO keyParameterLog) {
        keyParameterLogService.update(keyParameterLog);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("server:keyParameterLog:remove")
    public R remove(Long autoId) {
        if (keyParameterLogService.remove(autoId) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("server:keyParameterLog:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] autoIds) {
        keyParameterLogService.batchRemove(autoIds);
        return R.ok();
    }

}
